﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DigDes.DS2012.DataAccess;
using DigDes.DS2012.BusinessObjects;

namespace DigDes.DS2012.BusinessLogic
{
    public class DislocationHistoryManager : ManagerBase
    {
        private string fltCarNumber;
        private int fltOperation;
        private int fltStationOfOperation;
        private int fltArrivalStation;
        private int fltDepartureStation;

        public IEnumerable<string> GetReference(string storedProcedureName)
        {
            using (var command = new DislocationHistoryCommands())
            {
                using (var reader = command.GetReference(storedProcedureName))
                {
                    while (reader.Read())
                    {
                        yield return InitReference(reader);
                    }
                }
            }
        }

        public IEnumerable<DislocationHistory> GetDislocationHistory(string fltCarNumber, int fltOperation, int fltStationOfOperation, int fltArrivalStation, int fltDepartureStation)
        {
            this.fltCarNumber = fltCarNumber;
            this.fltArrivalStation = fltArrivalStation;
            this.fltOperation = fltOperation;
            this.fltDepartureStation = fltDepartureStation;
            this.fltStationOfOperation = fltStationOfOperation;

            using (var command = new DislocationHistoryCommands())
            {
                return command.RunInTransaction(GetDislocationHistory_res);
            }
        }

        private DislocationHistory[] GetDislocationHistory_res()
        {
            return GetDislocationHistory_Internal().ToArray();
        }

        private IEnumerable<DislocationHistory> GetDislocationHistory_Internal()
        {
            using (var command = new DislocationHistoryCommands())
            {
                using (var reader = command.GetDislocations(fltCarNumber, fltOperation, fltStationOfOperation, fltArrivalStation, fltDepartureStation))
                {
                    while (reader.Read())
                    {
                        yield return InitDislocationHistory(reader);
                    }
                }
            }
        }

        private string InitReference(SqlDataReader reader)
        {
            switch (reader.FieldCount)
            {
                case 2: return String.Format("{0}/{1}",
                            reader["Code"].ToString(),
                            (reader["Name"].ToString()) == "" ? "<не определено>" : reader["Name"].ToString());
                case 1: return reader["CarNumber"].ToString();
                default: return null;
            }
        }

        private DislocationHistory InitDislocationHistory(SqlDataReader reader)
        {
            return new DislocationHistory()
            {
                CarNumber = reader["CarNumber"].ToString(),
            //    RollCheck = Convert.ToInt32(reader["RollCheck"]),
            //    Lightweight = Convert.ToInt32(reader["Lightweight"]),
            //    KindOfCar = Convert.ToInt32(reader["KindOfCar"]),
            //    TypeOfConditionalOfCar = reader["TypeOfConditionalOfCar"].ToString(),
                StartDate = Convert.ToDateTime(reader["StartDate"]),
            //    FeatTracking = Convert.ToInt32(reader["FeatTracking"]),
            //    StateCode = Convert.ToInt32(reader["StateCode"]),
            //    ActualCargoWeight = Convert.ToInt32(reader["ActualCargoWeight"]),
                ArrivalStation = reader["ArrivalStation"].ToString(),
            //    CargoRate = reader["CargoRate"].ToString(),
            //    CargoOperative = reader["CargoOperative"].ToString(),
            //    Recipient = reader["Recipient"].ToString(),
            //    SpecialNotesOfCar = reader["SpecialNotesOfCar"].ToString(),
            //    NumberOfCaseCG = Convert.ToInt32(reader["NumberOfCaseCG"]),
            //    NumberOfCaseCP = Convert.ToInt32(reader["NumberOfCaseCP"]),
            //    NumberOfCaseKG = Convert.ToInt32(reader["NumberOfCaseKG"]),
            //    NumberOfCaseKP = Convert.ToInt32(reader["NumberOfCaseKP"]),
            //    ListNote = reader["ListNote"].ToString(),
            //    CarParkType = Convert.ToInt32(reader["CarParkType"]),
            //    FaultCode = Convert.ToInt32(reader["FaultCode"]),
                DepartureStation = reader["DepartureStation"].ToString(),
                Operation = reader["Operation"].ToString(),
                OperationDate = Convert.ToDateTime(reader["OperationDate"]),
                StationOfOperation = reader["StationOfOperation"].ToString(),
            //    DeliveryRoad = reader["DeliveryRoad"].ToString(),
            //    ReceptRoad = reader["ReceptRoad"].ToString(),
                TrainIndex = reader["TrainIndex"].ToString(),
                TrainNumber = reader["TrainNumber"].ToString()
            };
        }
    }
}
